#include "prospeckz_radio.h"
#include "prospeckz.h"
#include "MAC.h"

#include "lcd.h"

#include "defines.h"
#include "StopAndWait.h"

void (*MAC_ReceivedPacket)(void* data, unsigned int datalength);

void MAC_SetHandler(void (*X)(void*, unsigned int))
{
	MAC_ReceivedPacket = X;
}

unsigned char MAC_Buffer[MAC_MAX_PAYLOAD];
int MAC_CheckRadio(void)
{
	if (FIFOP_Data_ADDR & FIFOP_MASK) {
		
		struct xlowpan_addr64 src;
		unsigned int length = 0;
		int i = 0;

		receiveStatus_t result = Radio_receive(MAC_Buffer, MAC_MAX_PAYLOAD);
		
		lcd_set_pos(3,0);
		lcd_print_char('r');
		
		lcd_print_char('a');
		
		if(result.radioStatus == RADIO_SUCCESS)
		{		
			lcd_print_char('d');
			lcd_print_char('i');
			lcd_print_char('o');
			length = result.length;;
			MAC_ReceivedPacket(MAC_Buffer, length);
			length = xlowpan_recv(&src, MAC_Buffer, length);
			Reliable_ReceivePacket(&src, MAC_Buffer, length);
			
			return 1;
		}
		lcd_print_char('b');
		lcd_print_char('o');
		lcd_print_char('o');
	}

	return 0;
}

void MAC_Transmit( void* data,unsigned int maxlength)
{	
	Radio_transmit(data, maxlength);
}
